﻿using Microsoft.SharePoint.Client;

namespace ProvisioningLibrary.Handlers
{
    public class RoleHandler : Handler
    {
        public void CreateRole(ClientContext context, PermissionKind[] kinds, string roleName, string roleDescription)
        {
            if (ExistRole(context, roleName))
                return;

            RoleDefinitionCreationInformation info = new RoleDefinitionCreationInformation();
            info.BasePermissions = GetBasePermission(context, kinds);
            info.Description = roleDescription;
            info.Name = roleName;
            info.Order = 0;
            RoleDefinition role = context.Web.RoleDefinitions.Add(info);

            context.ExecuteQuery();
        }

        public void CreateGroup(ClientContext context, string groupName, string groupDescription)
        {
            context.Web.AddGroup(groupName, groupDescription, true);
        }

        public void AddUserToGroup(ClientContext context, string groupName, string userLoginName)
        {
            context.Web.AddUserToGroup(groupName, userLoginName);
        }

        protected BasePermissions GetBasePermission(ClientContext context, PermissionKind[] kinds)
        {
            BasePermissions perm = new BasePermissions();
            foreach (var kind in kinds)
                perm.Set(kind);
            return perm;
        }

        public void AddUserToRole(ClientContext context, string userLoginName, string roleName)
        {
            Principal user = GetUserByLoginName(context, userLoginName);
            AddPrincipalToRole(context, user, roleName);
        }

        public void AddGroupToRole(ClientContext context, string groupName, string roleName)
        {
            Principal group = GetGroupByName(context, groupName);
            AddPrincipalToRole(context, group, roleName);
        }

        protected void AddPrincipalToRole(ClientContext context, Principal principal, string roleName)
        {
            RoleDefinition role = GetRoleByName(context, roleName);
            if (principal != null && role != null)
            {
                RoleDefinitionBindingCollection roleBinding = new RoleDefinitionBindingCollection(context);
                roleBinding.Add(role);
                RoleAssignment assignment = context.Web.RoleAssignments.Add(principal, roleBinding);
                context.ExecuteQuery();
            }
        }

        protected bool ExistRole(ClientContext context, string roleName)
        {
            return GetRoleByName(context, roleName) != null;
        }

        protected Group GetGroupByName(ClientContext context, string groupName)
        {
            return context.Web.SiteGroups.GetByName(groupName);
        }

        protected User GetUserByLoginName(ClientContext context, string loginName)
        {
            return context.Web.SiteUsers.GetByLoginName(loginName);
        }

        protected RoleDefinition GetRoleByName(ClientContext context, string roleName)
        {
            return context.Web.RoleDefinitions.GetByName(roleName);
        }
    }
}